import { Injector, ElementRef } from '@angular/core';
import { AppConsts } from '@abp/AppConsts';
import { LocalizationService } from '@abp/localization/localization.service';
import { PermissionCheckerService } from '@abp/auth/permission-checker.service';
import { FeatureCheckerService } from '@abp/features/feature-checker.service';
import { NotifyService } from '@abp/notify/notify.service';
import { SettingService } from '@abp/settings/setting.service';
import { MessageService } from '@abp/message/message.service';
import { AbpMultiTenancyService } from '@abp/multi-tenancy/abp-multi-tenancy.service';
import { AbpSessionService } from '@abp/session/abp-session.service';
import { TableHelper } from './PrimengTableHelper';
import { NzDrawerService } from 'ng-zorro-antd';

import { Abp } from '@abp/Abp';



export abstract class AppComponentBase {

    localizationSourceName = AppConsts.localization.defaultLocalizationSourceName;

    localization: LocalizationService;
    permission: PermissionCheckerService;
    feature: FeatureCheckerService;
    notify: NotifyService;
    setting: SettingService;
    message: MessageService;
    multiTenancy: AbpMultiTenancyService;
    appSession: AbpSessionService;
    elementRef: ElementRef;
    tableHelper: TableHelper;
    drawerService: NzDrawerService;

    constructor(injector: Injector) {
        this.localization = injector.get(LocalizationService);
        this.permission = injector.get(PermissionCheckerService);
        this.feature = injector.get(FeatureCheckerService);
        this.notify = injector.get(NotifyService);
        this.setting = injector.get(SettingService);
        this.message = injector.get(MessageService);
        this.multiTenancy = injector.get(AbpMultiTenancyService);
        this.appSession = injector.get(AbpSessionService);
        this.elementRef = injector.get(ElementRef);
        this.tableHelper = new TableHelper();
        this.drawerService = injector.get(NzDrawerService);
    }

    l(key: string, ...args: any[]): string {

        let localizedText = this.localization.localize(key, this.localizationSourceName);

        if (!localizedText) {
            localizedText = key;
        }

        if (!args || !args.length) {
            return localizedText;
        }

        args.unshift(localizedText);
        return Abp.utils.formatString.apply(this, args);
    }

    isGranted(permissionName: string): boolean {
        return this.permission.isGranted(permissionName);
    }

    drawerConfig(nzTitle: string, nzContent: any, nzContentParams: any, nzWidth?: number, nzBodyStyle?: object, nzWrapClassName?: string,
        nzOffsetX?: number, nzMaskClosable: boolean = true): object {
        return {
            nzTitle: nzTitle,
            nzContent: nzContent,
            nzWidth: nzWidth == null ? 800 : nzWidth,
            nzBodyStyle: nzBodyStyle == null ? { height: 'calc(100% - 55px)', overflow: 'auto', 'padding-bottom': '53px' } : nzBodyStyle,
            nzContentParams: nzContentParams,
            nzWrapClassName: nzWrapClassName,
            nzOffsetX: nzOffsetX,
            nzMaskClosable: nzMaskClosable
        };
    }


    public get projectId(): number {
        return Number(sessionStorage.getItem('projectId'));
    }
}
